package com.mysql.study.cotroller.back;

import com.mysql.study.entity.Goods;
import com.mysql.study.entity.GoodsType;
import com.mysql.study.entity.User;
import com.mysql.study.service.GoodsTypeService;
import com.mysql.study.service.UserService;
import com.mysql.study.utils.UtilsTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *  用户(User)控制层
 */
@Controller
public class UserCotroller {
    //注解
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsTypeService typeService;
    @Autowired
    private UtilsTool utilsTool;
    @Autowired
    private GoodsCotroller gc;
    /**
     * 查询所出所有有用户,返回给网页
     * @return
     */
    @RequestMapping ("VAllUser")
    String FindUserAll(Model model) {
        List<User> user = FindUserAll();
        int user_number = user.size();
        model.addAttribute("AllUser", user);
        model.addAttribute("user_number", user_number);
        return "page/backstage/UserBack/VAllUser";
    }
    /**
     * 方便调用,让页面实时更新
     * @return
     */
    List<User> FindUserAll(){
        return this.userService.UserAll();
    }
    List<GoodsType> FindTypeAll(){return this.typeService.AllGoodsType();}

    /**
     * 添加用户与注册用户
     */
    @RequestMapping("addUser")
    String addString(User user, String password1, String admin, Model model){
        System.out.println(admin);
        if(admin == null){
            int num = this.userService.addUser(user, password1);
            if(num > 0){
                List<User> user1 = FindUserAll();
                int user_number = user1.size();
                model.addAttribute("AllUser", user1);
                model.addAttribute("user_number", user_number);
                return "page/backstage/UserBack/VAllUser";
            }else{
                model.addAttribute("Error","密码不一致请重新输入!");
                return "page/backstage/UserBack/addUser";
            }
        }
        model.addAttribute("admin", admin);
        return "page/backstage/UserBack/addUser";
    }

    /**
     *利用Id从数据库中删除用户
     * @param user_id
     * @return
     */
    @RequestMapping("deleteUser")
    String FindByIdDelete(String user_id,Model model){
        int id = Integer.parseInt(user_id);
        this.userService.deleteUserById(id);
        List<User> user1 = FindUserAll();
        int user_number = user1.size();
        model.addAttribute("AllUser", user1);
        model.addAttribute("user_number", user_number);
        return "page/backstage/UserBack/VAllUser";
    }

    /**
     * 查到对应id,并把值添加到编辑界面
     * @param user_id
     */
    @RequestMapping("editUser")
    String FidByIdEditUser(String user_id,Model model){
        int id = Integer.parseInt(user_id);
        User user1 = this.userService.EditUserById(id);
        model.addAttribute("user1", user1);
        return "page/backstage/UserBack/editUser";
    }
    /**
     *修改用户信息
     */
    @RequestMapping("editUserId")
    String EditUser(User user, String password1, Model model){
        int num = this.userService.EditUser(user, password1);
        if(num == 1) {
            List<User> user1 = FindUserAll();
            int user_number = user1.size();
            model.addAttribute("AllUser", user1);
            model.addAttribute("user_number", user_number);
            return "page/backstage/UserBack/VAllUser";
        }else {
            User user1 = this.userService.EditUserById(user.getUserId());
            model.addAttribute("user1", user1);
            model.addAttribute("Error", "密码不一致请重新输入!");
            return "page/backstage/UserBack/editUser";
        }
    }


    /**
     * 查询内容
     * @param content
     * @param model
     * @return
     */
    @RequestMapping("query")
    String FindQuery(String content, Model model){
        List<User> user1 = null;
        int user_number = 0;
        //输入内容为空直接返回全部
        if(content.equals("") || content == null){
            user1 = FindUserAll();
            user_number = user1.size();
        }else{
            //如果传过来的值为中文那么自动转为英文
            if(content.equals("管理员")){
                content = "root";
            }else if(content.equals("普通用户")){
                content = "common";
            }
            user1 = this.userService.FindQuery(content);
            user_number = user1.size();
        }
        //user_number则代表没有此数据,向前台发出提示
        if(user_number == 0){
            model.addAttribute("usernull", "该网站未能收入此信息,请参试添加");
        }
        model.addAttribute("AllUser", user1);
        model.addAttribute("user_number", user_number);
        return "page/backstage/UserBack/VAllUser";
    }

    /**
     * 用户登录加邮箱验证登
     * .....如果为root(1)用户则进入后台管理, 如果为common(2)用户则进入购物页面
     */
    //将数据封装易管理
    public String modelHtml(User getUser, Model model){
        if(getUser.getManage().equals("root")){
            model.addAttribute("nickName", getUser);
            return "page/backstage/index_back";
        }else{
            List<Goods> list1 = gc.FindGoodsAll();
            List<GoodsType> types = FindTypeAll();
            //将值传到前端
            model.addAttribute("list1",list1);
            model.addAttribute("types", types);
            model.addAttribute("nickName", getUser);
            return "page/front/index_front";
        }
    }


    //1.账号加密码进行登录操作
    @RequestMapping("login")
    String loginUserAll(User user, String verifycode, Model model, HttpServletRequest request){
        //如果手机都没填,直接返回登录界面

        String emailCode = (String)request.getSession().getAttribute("emailCode");
        User getUser = this.userService.loginUser(user);
        //如果没有此用户则直接返回登录界面
        if(getUser == null){
            return "page/user/user_login";
        }
        if(user.getEmail() == null){
            //验证密码,相同则登录, 不同则返回登录界面, 如果为root就进入后台管理界面,如果不是则进入前台
            if(getUser.getPassword().equals(user.getPassword())){
                String Mppings = modelHtml(getUser,model);
                return Mppings;
            }
        }else{
            //进行邮箱验证码登录
            System.out.println("email logins succeed:"+emailCode);
            if(verifycode.equals(emailCode)) {
                String Mppings = modelHtml(getUser, model);
                return Mppings;
            }
        }
        //初始界面
        return "page/user/user_login";
    }
    //2   120秒过后清除清除session缓存,清除emailcode
    @RequestMapping("clear")
    public String removeSession(HttpServletRequest request){
        System.out.println("清除sessiion");
        request.getSession().removeAttribute("emailCode");
        return "redirect:/login";
    }


    /**
     * 用户注册,验证码为60秒以内有效
     */
    @RequestMapping("send")
    String sendMail(String mail, HttpServletRequest request){
        if(!mail.equals("") && mail != null){
            String emailCode = String.valueOf(utilsTool.SendEmailVerifyCode(mail));
            request.getSession().setAttribute("emailCode", emailCode);
            System.out.println(emailCode+"     send");
            //return emailCode;
            return "redirect:/login";
        }
        return null;
    }

    /**
     *实现用户注册功能
     */
    @RequestMapping("email")
    String register(User user, String password1, String code, String mailcode, HttpServletRequest request) {
        System.out.println("aaa        "+request.getSession().getAttribute("emailCode"));
        if(mailcode != null){
            if(code.equals(mailcode)){
                user.setManage("common");
                int num = userService.addUser(user, password1);
                if (num > 0) {
                    System.out.println("用户添加成功");
                }else{
                    System.out.println("注册失败");
                }
                return "page/user/user_login";
            }
        }
        return "page/user/user_register";
    }
}


